$.namespace('oa.facility.workOrder');

oa.facility.workOrder = function(){
	return {
		init : function(stype){
 			oa.facility.workOrder.config.fm.layout.controls=oa.facility.workOrder.config.controls;
			
			EasyUIInit.initControl(this.config.fm);
			$('#fileUploadfm').ajaxForm({
				url: ctxRest + '/sys/file/upload',
				type: 'POST', 
				resetForm: true,
				beforeSend : function(){
					progressLoad();
				},
				success: function (data) {
					progressClose();
					$('#fileUploadDlg').dialog('close');
					var t = (new Date()).getTime();
		            $('#imgs').append('<span url="' + data + '"><img src="' + ctxRest + '/sys/file/get?path=' + data + '" style="width:100px;height:100px;" /><span style="position:relative;top:12px;left:-60px;"><a href="javascript:void(0)" onClick="$(this).parent().parent().remove()">删除</a></span></span>');
				}
			});
 
		},
	 
		loadRegion : function(obj, cb) {
			$.ajax({
				type : 'POST',
				url : ctxRest + '/gcb/grid/findList',
				contentType : 'application/json;charset=utf8',
				data : JSON.stringify({
					pid : obj,
					id	: ''
				}),
				success : function(result){
					console.log(result);
					$('#regionId').combobox('loadData', result);
					$('#regionId').combobox('reset');
					cb('ok');
				}
			})
		}  ,
		loadFacility : function(obj, cb) {
			$.ajax({
				type : 'POST',
				url : ctxRest + '/gcb/facility/findList',
				contentType : 'application/json;charset=utf8',
				data : JSON.stringify({
					paramMap:{regionID: $('#regionId').combobox('getValue'),type:$('#facilityType').combobox('getValue'),}
				}),
				success : function(result){
					console.log(result);
					//alert($('#facilityId'));
					result =result.rows;
					for (var i =0;i<result.length;i++)
						result[i].name = result[i].name +'(编号：'+result[i].code+')';
					$('#facilityId').combobox('loadData', result);
					$('#facilityId').combobox('reset');
					cb('ok');
				}
			})
		}  ,
		showFileUploadForm : function(){
			$('#fileUploadDlg').dialog('open');
		},
		getFormData : function(){
			var obj;
			var fm = $('#fm');
			if (fm.form('validate')) {
				obj = $.serializeObject(fm);
				var pic = '';
				$('#imgs').find('span').each(function(){
					if ($(this).attr('url')) {
						if (!pic) {
							pic = $(this).attr('url');
						} else {
							pic += (',' + $(this).attr('url'));
						}
					}
				});
				obj.pic = pic;
			}
			return obj;
		},
		create : function() {
			var obj = this.getFormData();//console.log(obj);return;
			//alert($('gridId').combobox('getText'));
			/*obj.actTitle = '（广场：' + $('#gridId').combobox('getText') + '）（区域：'
					+ $('#regionId').combobox('getText') + '）设施:'
					+ $('#facilityId').combobox('getText')+"：" + obj.content;*/
			obj.actTitle = '设施:' + $('#facilityId').combobox('getText') + "："
					+ obj.content;
			if(obj){
				$.ajax({
					type : 'POST',
					url	 : ctxRest + '/oa/facility/workorder/create',
					contentType : 'application/json;charset=utf-8',
					data : JSON.stringify(obj),
					beforeSend : function(){
						progressLoad();
					},
					success : function(result){
						progressClose();
						if (result && result.errcode == 0) {
							$.messager.alert('提示', '工单提交成功.', 'info');
							$('#fm').form('clear');
							$('#imgs').children().remove();
						}
					}
				})
			}
		},
		cancel : function(){
			$('#fm').form('clear');
			$('#imgs').children().remove();
		}
	}
}();

oa.facility.workOrder.config = {
	fm : {
		id : 'fm',
		isExternal : true,
		type : 'form',
		style : "padding:15px;",
		layout : {
			captionWidth : 100,
			fieldWidth : 200,
			fieldHeight : 30,
			rowHeight : 40,
			columns : 2,
			controls:[]
		}
	},
 
	controls : [
				{id:'gridId',caption:'广场名称',type:'easyui-combobox',option:{
					valueField:'id',
					textField:'name',
					required:true,
					editable:false,
					url : ctxRest + '/gcb/grid/findList',
					contentType : 'application/json;charset=utf8',
					queryParams : {
						pid : '0'
					},
					onChange : function(newValue, oldValue){//console.log(newValue);
						oa.facility.workOrder.loadRegion(newValue, function(res){});
					}
				}},
				{id:'regionId',caption:'广场区域',type:'easyui-combobox',option:{valueField:'id',textField:'name',required:true,editable:false,
					onChange : function(newValue, oldValue){//console.log(newValue);
						oa.facility.workOrder.loadFacility(newValue, function(res){});
					}
				}},
				{id:'facilityType',caption:'设施类型',type:'easyui-combobox',option:{valueField:'code',textField:'name',url:ctxRest+'/sys/basecode/get?classCode=设施类型',method:'GET',required:true,editable:false,
					onChange : function(newValue, oldValue){//console.log(newValue);
						oa.facility.workOrder.loadFacility(newValue, function(res){});
					}
				}},
				{id:'facilityId',caption:'设施名称',type:'easyui-combobox',option:{valueField:'id',textField:'name',required:true,editable:false,panelWidth:300}},
				{id:'type',caption:'业务类型',type:'easyui-combobox',option:{valueField:'code',textField:'name',url:ctxRest+'/sys/basecode/get?classCode=wo_type',method:'GET',required:true,editable:false}},
				{id:'source',caption:'案件来源',type:'easyui-combobox',colspan:1,option:{valueField:'code',textField:'name',url:ctxRest+'/sys/basecode/get?classCode=wo_source',method:'GET',required:true,editable:false}},
				{id:'createTime',caption:'工单时间',type:'easyui-datetimebox',option:{required:true,editable:false}},
				{id:'limitHour',caption:'期限(小时)',type:'easyui-numberspinner',option:{required:true,min:1,max:100}},
				{id:'content',caption:'内容',type:'easyui-textbox',colspan:2,option:{height:60,multiline:true,required:true}},
				{id:'btn',caption:'上传图片',type:'easyui-linkbutton',attr:'onClick="oa.facility.workOrder.showFileUploadForm()"',option:{width:80,text:'上传图片'},colspan:2},
				{id:'imgs',caption:'图片',captionVisible:true,type:'div',isExternal:true,colspan:2},
				{id:'div_button',caption:'',captionVisible:true,type:'span',colspan:2,isExternal:true,style:'text-align: center',
					controls:[
			          	{id:'lb_ok',    caption:'',captionVisible:false,type:'easyui-linkbutton',style:'padding:2px',attr:'onClick="oa.facility.workOrder.create()"',option:{text:"确定",width:80}},
			          	{id:'lb_cancle',caption:'',captionVisible:false,type:'easyui-linkbutton',style:'padding:2px',attr:'onClick="oa.facility.workOrder.cancel()"',option:{text:"取消",width:80}}
			          ]	
				}
			]
}

$(function(){

	oa.facility.workOrder.init();
})